﻿using EmitMapper;
using EmitMapper.MappingConfiguration;
using LightDataAccess;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebDemo.Data;
using WebDemo.Domain;
using WebDemo.Filters;
using WebDemo.Models;

namespace WebDemo.Controllers
{
    /// <summary>
    /// GPS消息接口控制器
    /// </summary>
    public class GPSMsgController : ApiController
    {
        private GPSRepository m_gpsRepository = new GPSRepository();

        /// <summary>
        /// 创建一个GPS跟踪记录
        /// POST api/GPSMsg
        /// </summary>
        /// <param name="msg">GPS消息体</param>
        /// <returns>HTTP响应</returns>
        public HttpResponseMessage Post(GPSMsg msg)
        { 
            ObjectsMapper<GPSMsg, GpsTrackInfo> mapper = ObjectMapperManager.DefaultInstance.GetMapper<GPSMsg, GpsTrackInfo>(
                new DefaultMapConfig()
                .MatchMembers((f, d) => d.ToLower().StartsWith(f) || (d == "GPSTime" && f == "time"))
                .PostProcess<GpsTrackInfo>((d, s) => { d.InputTime = DateTime.Now; return d; })
                );
            var result = mapper.Map(msg);
            
            m_gpsRepository.Create(result);

            return Request.CreateResponse(HttpStatusCode.Created, msg);
        }

        /// <summary>
        /// 获取一条GPS消息
        /// GET api/GPSMsg
        /// </summary>
        /// <returns>GPS消息</returns>
        public GPSMsg Get()
        {
            var domain = m_gpsRepository.SelectTopNRecords(1).FirstOrDefault();

            ObjectsMapper<GpsTrackInfo, GPSMsg> mapper = ObjectMapperManager.DefaultInstance.GetMapper<GpsTrackInfo, GPSMsg>(
                new DefaultMapConfig()
                .MatchMembers((f, d) => f.ToLower().StartsWith(d) || (f == "GPSTime" && d == "time")) //匹配方法1.小写左Like 2.特定字段指定
                );

            var result = mapper.Map(domain);

            return result;
        }

        [Profile]
        public IEnumerable<GpsTrackInfo> GetTop(int id)
        {
            return m_gpsRepository.SelectTopNRecords(id);
        }


        [Profile]
        public IEnumerable<GpsTrackInfo> GetByAddress(string address, int count)
        {
            return m_gpsRepository.SelectByAddress(address);
        }

        public HttpResponseMessage Put(string id, GPSMsg msg)
        {
            return Request.CreateResponse(HttpStatusCode.Accepted, msg);
        }

        public HttpResponseMessage Delete(string id)
        {
            return Request.CreateResponse(HttpStatusCode.Accepted, id);
        }


        //[Profile]
        //[HttpPost]
        //public IEnumerable<GpsTrackInfo> PostByQuery(GPSQuery query)
        //{
        //    return m_gpsRepository.SelectByAddress("");
        //}
    }
}
